package com.yc.hwc.web;

import java.util.Map;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.multipart.MultipartFile;

import com.yc.hwc.bean.Result;
import com.yc.hwc.bean.User;
import com.yc.util.Utils;

@RestController
@RequestMapping("js/ueditor-1.4.3.3/ctrl")
public class UeditorDo {

	@Resource
	UploadDo udo;

	@RequestMapping(params = "action=config")
	public Map<?, ?> config() {
		return Utils.toMap(
				"snapscreenActionName", "upimg",
				"imageActionName", "upimg", 
				"imageUrlPrefix", "",
				"imagePath", "/", 
				"imageFieldName", "file",
				"imageMaxSize", 1024*1024*5, 
				"imageAllowFiles", new String[] { ".png", ".jpg", ".jpeg", ".gif", ".bmp" });
	}

	@RequestMapping(params = "action=upimg")
	public Map<?, ?> upimg(@RequestParam("file") MultipartFile mf, @SessionAttribute(required = false) User loginedUser) {
		Result<?> ret = udo.upload(mf, loginedUser);
		if(ret.getCode()==0) {
			Map<?,?> u = (Map<?,?>) ret.getData();
			return Utils.toMap(
					"state", "SUCCESS",
				    "url", u.get("webPath"),
				    "title", u.get("newName"),
				    "original", u.get("oldName"));
		} else {
			return Utils.toMap("state",ret.getMsg());
		}
	}

}
